package com.example.datascreen.service.Impl;

import com.example.datascreen.entity.Essay;
import com.example.datascreen.mapper.EssayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EssayServiceImpl {
    @Autowired
    EssayMapper essayMapper;
    public List<List> getRjDaychart() {
        Map map = new HashMap();
        Map map1 = new HashMap();
        List<Map> mapList = new ArrayList();
        List<List> list = new ArrayList();
        map.put("name", "机器人");
        map.put("value", this.essayMapper.getRobotNum());
        map1.put("name", "人工");
        map1.put("value", this.essayMapper.getAllNum() - this.essayMapper.getRobotNum());
        mapList.add(map);
        mapList.add(map1);
        list.add(mapList);
        return list;
    }

    public Map getEssayPie() {
        List<Essay> list = essayMapper.getEssayPieList();
        Map<String,Integer> map = new HashMap<>();
        for (Essay essay : list) {
            if(!map.containsKey(essay.getColumn())){
                map.put(essay.getColumn(),1);
            }else {
                Integer temp = map.get(essay.getColumn());
                temp++;
                map.put(essay.getColumn(),temp);
            }
        }
        return map;
    }

    public Map getLineMap(){
        List<Essay> Cdcj = essayMapper.getCdcjLineList();
        List<Essay> Qqcj = essayMapper.getQqcjLineList();
        List<Essay> Hgjj = essayMapper.getHgjjLineList();
        List<Essay> Whsc = essayMapper.getWhscLineList();
        List<Essay> Sccj = essayMapper.getSccjLineList();
        List<Essay> Hbsc = essayMapper.getHbscLineList();
        List<Integer> timeList = new ArrayList<>();
        List<Integer> CdcjList = new ArrayList<>();
        List<Integer> QqcjList = new ArrayList<>();
        List<Integer> HgjjList = new ArrayList<>();
        List<Integer> WhscList = new ArrayList<>();
        List<Integer> SccjList = new ArrayList<>();
        List<Integer> HbscList = new ArrayList<>();

        CdcjList = getData(Cdcj);
        QqcjList = getData(Qqcj);
        HgjjList = getData(Hgjj);
        WhscList = getData(Whsc);
        SccjList = getData(Sccj);
        HbscList = getData(Hbsc);
        for (int i = 0; i <12 ; i++) {
            timeList.add(i+1);
        }

        Map map = new HashMap<>();
        map.put("CdcjList",CdcjList);
        map.put("QqcjList",QqcjList);
        map.put("HgjjList",HgjjList);
        map.put("WhscList",WhscList);
        map.put("SccjList",SccjList);
        map.put("HbscList",HbscList);
        map.put("timeList",timeList);
        return map;
    }


    public List<Integer> getData(List<Essay> x){
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        int count5 = 0;
        int count6 = 0;
        int count7 = 0;
        int count8 = 0;
        int count9 = 0;
        int count10 = 0;
        int count11 = 0;
        int count12 = 0;
        for (Essay cdcjEssay : x) {
            switch (cdcjEssay.getTime()){
                case 1:
                    count1++;
                    break;
                case 2:
                    count2++;
                    break;
                case 3:
                    count3++;
                    break;
                case 4:
                    count4++;
                    break;
                case  5:
                    count5++;
                    break;
                case 6:
                    count6++;
                    break;
                case 7:
                    count7++;
                    break;
                case 8:
                    count8++;
                    break;
                case 9:
                    count9++;
                    break;
                case 10:
                    count10++;
                    break;
                case 11:
                    count11++;
                    break;
                case 12:
                    count12++;
                    break;
            }
        }
        List<Integer> temp = new ArrayList<>();
        temp.add(count1);
        temp.add(count2);
        temp.add(count3);
        temp.add(count4);
        temp.add(count5);
        temp.add(count6);
        temp.add(count7);
        temp.add(count8);
        temp.add(count9);
        temp.add(count10);
        temp.add(count11);
        temp.add(count12);
        return temp;
    }

    public List<List> getBarData(){
        List<List> lists = new ArrayList<>();
        List<String> name = essayMapper.getAuthor();
        List<String> head = new ArrayList<>();
        head.add("score");
        head.add("amount");
        head.add("product");
        lists.add(head);
        for (String s : name) {
            List<String> list = new ArrayList<>();
            list.add("3");
            list.add(essayMapper.getNumByAuthor(s)+"");
            list.add(s);
            lists.add(list);
        }
        return lists;
    }


    public Map<String, List<Object>> getBooklist() {
        Map map = new HashMap<>();
        List<String> columns = new ArrayList<>();
        List<Integer> original = new ArrayList<>();
        List<Integer> editor = new ArrayList<>();
        List<Integer> translator = new ArrayList<>();

        List<Essay> books = essayMapper.getBooklist();

        for (Essay book : books) {
            if (!columns.contains(book.getColumn())) {
                columns.add(book.getColumn());
            }
        }

        for (String column : columns) {
            int originalCount = 0;
            int editorCount = 0;
            int translatorCount = 0;

            for (Essay book : books) {
                if (book.getColumn().equals(column)) {
                    switch (book.getAuthor_type()) {
                        case "原创":
                            originalCount++;
                            break;
                        case "编辑":
                            editorCount++;
                            break;
                        case "翻译":
                            translatorCount++;
                            break;
                    }
                }
            }
            original.add(originalCount);
            editor.add(editorCount);
            translator.add(translatorCount);
        }

        map.put("columns", columns);
        map.put("original", original);
        map.put("editor", editor);
        map.put("translator", translator);

        return map;
    }

    public Map getEssayData(){
        List<Essay> essayList = essayMapper.getEssayList();
        List<String> columnData = new ArrayList<>();
        List<Integer> Inquiries = new ArrayList<>();
        List<Integer> News = new ArrayList<>();
        List<Integer> Alerts = new ArrayList<>();

        for (Essay essay : essayList) {
            columnData.add(essay.getColumn());
        }
        List<String> List1 = new ArrayList<>(columnData.size());
        columnData.forEach(i -> {
            if (!List1.contains(i)) {
                List1.add(i);
            }
        });
        for (String column : List1) {
            int InquiriesCount = 0;
            int NewsCount = 0;
            int AlertsCount = 0;

            for (Essay essay : essayList) {
                if (essay.getColumn().equals(column)) {
                    switch (essay.getNews_type()) {
                        case "资讯":
                            InquiriesCount++;
                            break;
                        case "要闻":
                            NewsCount++;
                            break;
                        case "快讯":
                            AlertsCount++;
                            break;
                    }
                }
            }
            Inquiries.add(InquiriesCount);
            News.add(NewsCount);
            Alerts.add(AlertsCount);
        }
        Map map = new HashMap();
        map.put("Inquiries", Inquiries);
        map.put("News", News);
        map.put("Alerts", Alerts);
        map.put("List1",List1);
        return map;
    }
}
